#pragma once
template<class K >
struct BSTreeNode
{
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key;
	BSTreeNode(const K& key)
		:_key(key)
		,_left(nullptr)
		,_right(nullptr)
	{}
};


template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	BSTree()
		:_root(nullptr)
	{}
	//拷贝构造函数
	BSTree(const BSTree<K>& t)
	{
		//利用递归的方式进行构造
		_root = Copy(t._root);
	}
	BSTree<K>& operator=(BSTree<K> t)
	{
		swap(_root, t._root);
		return *this;
	}
	~BSTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	bool Insert(const K& key)
	{
		if (!_root)
		{
			_root = new Node(key);
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
				return false;
		}
			cur = new Node(key);
			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}
		
		return true;
	}
	void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}

	void Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}
	bool Erase(const K& key)
	{
		//1、首先需要找到这个节点
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			//找到该节点了
			else
			{
				//找到节点，开始进行删除，也得找到父节点
				//1、cur左为空,parent指向右  需要判断是否是root
				//2、cur右为空，parent指向左,需要判断是否是root
				//3、左右都不为空
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;

				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
						else
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
				}
				else   //左右都不为空
				{
					//去找右子树的最小值
					Node* minRight = cur->_right;
					Node* parent = cur;
					while (minRight->_left)
					{
						parent = minRight;
						minRight = minRight->_left;
					}
					//交换值
					cur->_key = minRight->_key;
					if (minRight == parent->_left)  //说明是根节点
					{
						parent->_left = minRight->_right;
					}
					else
					{
						parent->_right = minRight->_right;
					}
					delete minRight;
				}
				return true;
			}
		}
		return false;

	}
	//递归的方式实现Find
	bool FindR(const K& key)
	{
		return _FindR(_root, key);
	}

	//递归的方式实现INsert
	bool InsertR( const K& key)
	{
		return _InsertR(_root,key);
	}


	bool EraseR(const K& key)
	{
		return _EraseR(_root, key);
	}
private:
	bool _FindR(Node* root , const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		else if (root->_key > key)
		{
			return _FindR(root->_left, key);
		}
		else if (root->_key < key)
		{
			return _FindR(root->_right, key);
		}
		else
		{
			return true;
		}
	}
	bool _InsertR(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}
		else if (root->_key > key)
		{
			return _InsertR(root->_left, key);
		}
		else if (root->_key < key)
		{
			return _InsertR(root->_right, key);
		}
		else 
		{
			return false;
		}
	}

	void _Inorder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_Inorder(root->_left);
		cout << root->_key << " ";
		_Inorder(root->_right);
	}

	void Destroy(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}

	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* newnode = new Node(root->_key);
		newnode->_left = Copy(root->_left);
		newnode->_right = Copy(root->_right);
		return newnode;
	}

	bool _EraseR(Node* root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key > key)
		{
			_EraseR(root->_left, key);
		}
		else if (root->_key < key)
		{
			_EraseR(root->_right, key);
		}
		else
		{
			Node* del = root;
			//首先确定左边为空，或者右边为空的这两种情况
			if (root->_left == nullptr)
			{
				root = root->_left;
			}
			else if (root->_right == nullptr)
			{
				root = root->right;
			}
			else
			{
				Node* minRight = root->_right;
				while (minRight->_left)
				{
					minRight = minRight->_left;
				}
				return _EraseR(root->_right, key);
			}
			delete del;
			return true;
		}
	}
private:
	Node* _root = nullptr;
};



namespace KV {

	template<class K, class V>
	struct BSTNode
	{
		BSTNode(const K& key = K(), const V& value = V())
			: _pLeft(nullptr), _pRight(nullptr), _key(key), _Value(value)
		{}
		BSTNode<T>* _pLeft;
		BSTNode<T>* _pRight;
		K _key;
		V _value
	};
	template<class K, class V>
	class BSTree
	{
		typedef BSTNode<K, V> Node;
		typedef Node* PNode;
	public:
		BSTree() : _pRoot(nullptr) {}
		PNode Find(const K& key);
		bool Insert(const K& key, const V& value)
			bool Erase(const K& key)
	private:
		PNode _pRoot;
	};

	void TetsBSTree2()
	{
		//词库中单词都放进这个搜索树中
		//Key的搜索模型，判断在不在
		//场景：检查单鞋拼写是否正确/车库出入系统
	}

}